package common

import (
	"context"
	"github.com/gin-gonic/gin"
	"github.com/redis/go-redis/v9"
	"gorm.io/gorm"
)

type BaseLogic struct {
	*ServiceContext
}

// 从 gin.Context 获取已经注入的 ServiceContext
func NewBaseLogic(c *gin.Context) *BaseLogic {
	svcAny, _ := c.Get("svc") // svc 是在 ServiceContextMiddleware 注入的
	svc := svcAny.(*ServiceContext)
	return &BaseLogic{svc}
}

// ============ 方便调用的 getter ============

func (l *ServiceContext) SetLog() *Logger {
	return l.LogClient
}

func (l *ServiceContext) GetRedis() *redis.Client {
	return l.RedisClient
}

func (l *ServiceContext) GetDB() *gorm.DB {
	return l.DbClient
}

func (l *ServiceContext) GetGinCtx() *gin.Context {
	return l.GinCtx
}

func (l *ServiceContext) GetCtx() context.Context {
	return l.GinCtx.Request.Context()
}
